package com.java.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.java.entity.Timeslot;
import com.java.utils.SqlCons;

/**
* @ClassName: TimeslotDao
* @Description: 坐诊数据库操作
* @author ChenJiaLu
* @date 2023年12月29日 上午10:46:03
*
 */
public class TimeslotDao extends BaseDao {

	public List<Timeslot> getTimeslots(){
		List<Timeslot> list = new ArrayList<Timeslot>();
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.TIMESLOT.SELECT_ALL);
			ResultSet res = statement.executeQuery();
			while(res.next()) {
				Timeslot info = new Timeslot();
				info.setId(res.getInt("id"));
				info.setId(res.getInt("doctorid"));
				info.setDay1(res.getInt("day1"));
				info.setDay2(res.getInt("day2"));
				info.setDay3(res.getInt("day3"));
				info.setDay4(res.getInt("day4"));
				info.setDay5(res.getInt("day5"));
				info.setDay6(res.getInt("day6"));
				info.setDay7(res.getInt("day7"));
				list.add(info);
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
		return list;
	}

	public Timeslot getDoctorTimeslot(int doctorid){
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.TIMESLOT.SELECT_DOCTOR);
			statement.setInt(1, doctorid);
			ResultSet res = statement.executeQuery();
			if(res.next()) {
				Timeslot info = new Timeslot();
				info.setId(res.getInt("id"));
				info.setId(res.getInt("doctorid"));
				info.setDay1(res.getInt("day1"));
				info.setDay2(res.getInt("day2"));
				info.setDay3(res.getInt("day3"));
				info.setDay4(res.getInt("day4"));
				info.setDay5(res.getInt("day5"));
				info.setDay6(res.getInt("day6"));
				info.setDay7(res.getInt("day7"));
				return info;
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
		return null;
	}

	public void delTimeslot(int id) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.TIMESLOT.DELETE);
			statement.setInt(1, id);
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}

	public void saveTimeslot(Timeslot timeslot) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.TIMESLOT.INSERT);
			statement.setInt(1, timeslot.getDoctorid());
			statement.setInt(2, timeslot.getDay1());
			statement.setInt(3, timeslot.getDay2());
			statement.setInt(4, timeslot.getDay3());
			statement.setInt(5, timeslot.getDay4());
			statement.setInt(6, timeslot.getDay5());
			statement.setInt(7, timeslot.getDay6());
			statement.setInt(8, timeslot.getDay7());
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}

	public void updateTimeslot(Timeslot timeslot) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.TIMESLOT.UPDATE);
			statement.setInt(1, timeslot.getDoctorid());
			statement.setInt(2, timeslot.getDay1());
			statement.setInt(3, timeslot.getDay2());
			statement.setInt(4, timeslot.getDay3());
			statement.setInt(5, timeslot.getDay4());
			statement.setInt(6, timeslot.getDay5());
			statement.setInt(7, timeslot.getDay6());
			statement.setInt(8, timeslot.getDay7());
			statement.setInt(9, timeslot.getId());
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}
}
